﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Commons.DataFilters;
using SD.Business;
using SD.DataAccess;
namespace SD.Web.Controls.Lookup
{

    public partial class AddressLookupControl : System.Web.UI.UserControl
    {
        /// <summary>
        /// AddressFilter property
        /// </summary>
        public AddressFilter AddressFilter {

            get
            {
                AddressFilter filter = null;

                if (ViewState["AddressFilter"] == null)
                {
                    this.AddressFilter = filter = new AddressFilter();
                }
                else
                {
                    filter = ViewState["AddressFilter"] as AddressFilter;
                }

                return filter;

            }
            protected set
            {
                ViewState["AddressFilter"] = value;
            }
        
        }

        public event EventHandler<SelectedValueEventArgs> SelectedValue;

        public event EventHandler<EventArgs> ClickedNoneButton;

        /// <summary>
        /// Event is raised after users click None button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnClickedNoneButton(object sender, EventArgs e)
        {
            if (ClickedNoneButton != null)
            {
                ClickedNoneButton(this, e);
            }
        }

        /// <summary>
        /// Event is raised after users click select button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnSelectedValue(object sender, SelectedValueEventArgs e)
        {
            if (SelectedValue != null)
            {
                SelectedValue(sender, e);
            }
        }

        /// <summary>
        /// Method for poping up a window which contains this control
        /// </summary>
        public void Popup()
        {
            PanelContainer.CssClass = "popup_container";
            PanelContainer.Visible = true;

        }
        /// <summary>
        /// event is raised when users click on search button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmdSelect_Click(object sender, EventArgs e)
        {
            var addressId = 0;
            if (int.TryParse(Request.Form["RadioButton"], out addressId))
            {
                PanelContainer.Visible = false;

                var addressDetail = LookupController.GetAddressDetail(addressId);
                var args = new SelectedValueEventArgs(addressDetail);
                OnSelectedValue(this, args);
            }
        }

        /// <summary>
        /// Event is raised when users click on close button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmdClose_Click(object sender, EventArgs e)
        {

            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Event is raised when users click on search button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            this.AddressFilter.PostCode = txtPostcode.Text.Trim();
            this.AddressFilter.Street = txtStreet.Text.Trim();
            this.AddressFilter.Town = txtTown.Text.Trim();
            
            grvSearchPostcode.DataSourceID = "PostCodeObjectDataSource";
            grvSearchPostcode.DataBind();
        }
        
        /// <summary>
        /// Event is raised when users click on none button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmdNone_Click(object sender, EventArgs e)
        {
            PanelContainer.Visible = false;
            OnClickedNoneButton(this, null);
        }

        /// <summary>
        /// Event is raised when users click on clear button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmdClear_Click(object sender, EventArgs e)
        {
            txtPostcode.Text = string.Empty;
            txtStreet.Text = string.Empty;
            txtTown.Text = string.Empty;
        }

        /// <summary>
        /// Event raised when query database from databases
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PostCodeObjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["filter"] = AddressFilter;
        }

    }

    public class SelectedValueEventArgs : EventArgs
    {
        public Address Address { get; set; }

        public SelectedValueEventArgs(Address address)
        {
            this.Address = address;
        }
    }
}